// pages/my/qrcode/qrcode.js
const App = getApp()

const api = require('../../../utils/api.js');
var util = require('../../../utils/util.js');


var interval;//定时器

Page({
  data: {

    code: null,
    cash: 0,//零钱余额

    show: !0,
    form: {
      name: '',
      is_def: !1,
    },
  },


  onLoad(options) {

    this.setData({
      cash: options.cash
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    const that = this

    //先调用一次
    this.getBarCode()
    this.getQrCode()
    //定时器
    interval = setInterval(function () {
      console.log("定时器" + interval)
      //每隔一分钟定时调用  付款码条码:
      that.getBarCode()
      that.getQrCode()
    }, 1000 * 60)

  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(interval);//页面卸载时 消毁定时器
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(interval);//页面隐藏时 消毁定时器
  },


  getBarCode: function (e) {
    const paramsData = {
      type: 'T',
      member_type_key: 'consumer',
      member_id: App.globalData.member_id,
    }
      //付款码条码
    const data = {
      service: 'trade.tradeCodeRegister',
      params: JSON.stringify(paramsData),
      url_type: 'finance',

      app_token: App.globalData.app_token,         
      security_code: App.globalData.security_code

    }

    console.log("....data>>>");
    console.log(data)

    api.requestCode({//注意是requestCode方法
      data,
      success: (res) => {
        console.log("....res.data>>>" + res.data);
        var resData = res.data;
        console.log(resData);

        this.setData({
          code: resData.data.security_code
        })



        util.showBrcode('barcode', resData.data.security_code, 560, 120);
      },
      fail: (res) => {
        console.log(res.data);
      }
    })

  },



  getQrCode: function (e) {

    const data = {
      url_type: '/openapi/trade/reg',
      app_token: App.globalData.app_token,
      security_code: App.globalData.security_code  
    }

    api.reqData({
      data,
      success: (res) => {
        console.log("..QrCode..res.data>>>");

        console.log(res.data)
        console.log(res.data.data.flow_no)
        console.log(res.data.data.security_code)

        var resData = res.data;
        console.log(resData);

        if (resData.rsp_code != 'fail') {
          this.setData({
            flow_no: res.data.data.flow_no,
            security_code: res.data.data.security_code
          })

          const paramsData = {
            amount: this.data.cash,
            type: 'P',
            member_type_key: 'consumer',
            member_id: App.globalData.memberInfo.member_id,
            security_code: res.data.data.security_code,
            flow_no: res.data.data.flow_no,
          }

          console.log(">>>paramsData>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
          console.log(JSON.stringify(paramsData))

          util.showQrcode('qrcode', JSON.stringify(paramsData), 350, 350);
        }

      },
      fail: (res) => {
        console.log(res.data);
      }
    })
  },




  toSetMoney(e) {
    App.WxService.navigateTo('/pages/my/qrcode/setmoney?cash=' + this.data.cash)
  },


  toSetArea(e) {
    console.log(e)
    App.WxService.navigateTo('/pages/my/area/index')
  }

})